//
//  ChangeAddressNewsController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/10.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChangeAddressNewsController.h"
#import "AddressPickerController.h"

@interface ChangeAddressNewsController ()<AddressPickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *ChangeBackView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *BackViewTopH;
@property (weak, nonatomic) IBOutlet UITextField *UserName;
@property (weak, nonatomic) IBOutlet UITextField *UseriPhone;
@property (weak, nonatomic) IBOutlet UILabel *UserLocation01;
@property (weak, nonatomic) IBOutlet UITextField *UserLocation02;
@property (weak, nonatomic) IBOutlet UISwitch *FirstLocation;



@end

@implementation ChangeAddressNewsController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"编辑地址";
    
    self.BackViewTopH.constant = -TopH;
    UIButton * leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"MainBackImage_W"] forState:UIControlStateNormal];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(5,0,5,20);
    [leftBtn addTarget:self action:@selector(ClickOnBtnOfLeftItem) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem * RightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(ClickOnBtnOfRightItem)];
    RightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = RightItem;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,KScreenW,TopH);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:109/255.0 blue:1/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:41/255.0 blue:67/255.0 alpha:1].CGColor];
    gl.locations = @[@(0.0),@(1.0f)];
    [self.ChangeBackView.layer addSublayer:gl];
    
    self.UserName.text = [NSString stringWithFormat:@"%@",self.ChangeModel.address_name];
    self.UseriPhone.text = [NSString stringWithFormat:@"%@",self.ChangeModel.address_phone];
    self.UserLocation01.text = [NSString stringWithFormat:@"%@",self.ChangeModel.address_diqu];
    self.UserLocation02.text = [NSString stringWithFormat:@"%@",self.ChangeModel.address_xx];
    if ([self.ChangeModel.address_default isEqual:@"1"]) {
        self.FirstLocation.on = YES;
    }else{
        self.FirstLocation.on = NO;
    }
}

- (IBAction)ClickOnBtnToChooseAddress:(id)sender {
    
    AddressPickerController * PickC = [[AddressPickerController alloc] init];
    PickC.delegate = self;
    PickC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:PickC animated:YES completion:nil];
    
}
- (IBAction)ClickOnSwitchToChooseFirstAddress:(UISwitch *)sender {
    if (sender.on) {
        NSLog(@"dakai");
        
        
    }else {
        NSLog(@"guangbi");
    }
    
}

- (void)ClickOnBtnToUploadChooseAddressString:(NSString *)AddressStr{
    
    if (AddressStr.length > 0) {
        self.UserLocation01.text = AddressStr;
    }
}



- (IBAction)ClickOnBtnToRemoveTheLocation:(id)sender {
    
    UIAlertController * Alert = [UIAlertController alertControllerWithTitle:@"" message:@"确认删除改地址？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * Action = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [MBProgressHUD showAlertViewWithText:@"删除本条信息"];
        
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
        [dic setObject:self.ChangeModel.address_id forKey:@"address_id"];
        [MainObjectWay PostWithMethod:@"del_address" andAction:@"Pos" andAddParam:YES parameters:dic success:^(id responseObject) {
            NSLog(@"删除返回信息 = %@",responseObject);
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
            if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(NSError *error) {
            NSLog(@"错误信息 = %@",error);
        }];
    }];
    [Action setValue:[UIColor redColor] forKey:@"_titleTextColor"];
    [Alert addAction:Action];
    
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"-----");
    }]];
    
    
    [self presentViewController:Alert animated:YES completion:nil];
}

- (void)ClickOnBtnOfLeftItem{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)ClickOnBtnOfRightItem{
    
    if (self.UserName.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"收货人姓名不能为空"];
        return;
    }
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhone.text]) {
        [MBProgressHUD showAlertViewWithText:@"请填写正确的手机号"];
        return;
    }
    
    if (self.UserLocation01.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择收货地址"];
        return;
    }
    if (self.UserLocation02.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写详细地址"];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.ChangeModel.address_id forKey:@"address_id"];
    [dic setObject:self.UserName.text forKey:@"address_name"];
    [dic setObject:self.UseriPhone.text forKey:@"address_phone"];
    [dic setObject:self.UserLocation01.text forKey:@"address_diqu"];
    [dic setObject:self.UserLocation02.text forKey:@"address_xx"];
    if (self.FirstLocation.on) {
        [dic setObject:@"1" forKey:@"address_default"];
    }else{
        [dic setObject:@"0" forKey:@"address_default"];
    }
    [MainObjectWay PostWithMethod:@"edit_address" andAction:@"Pos" andAddParam:YES parameters:dic success:^(id responseObject) {
        
        NSLog(@"修改信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [MBProgressHUD showAlertViewWithText:@"修改失败"];
    }];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
